require 'mkmf'

have_flags = %w[
  OPENSSL_110_THREADING_API
  HMAC_CTX_COPY
  EVP_CIPHER_CTX_COPY
  EVP_DIGESTFINAL_EX
  BN_RAND_RANGE
  BN_PSEUDO_RAND_RANGE
  X509V3_EXT_NCONF_NID
  X509_NAME_HASH_OLD
  OBJ_NAME_DO_ALL_SORTED
  PKCS5_PBKDF2_HMAC
  PKCS5_PBKDF2_HMAC_SHA1
  OPENSSL_ENGINE_H
  ENGINE_ADD
  ENGINE_LOAD_BUILTIN_ENGINES
  ENGINE_GET_DIGEST
  ENGINE_GET_CIPHER
  ENGINE_CLEANUP
  ENGINE_LOAD_DYNAMIC
  ENGINE_LOAD_CRYPTODEV
  ENGINE_LOAD_AESNI
  EVP_CIPHER_CTX_ENGINE
  AUTHENTICATED_ENCRYPTION
  OPENSSL_OCSP_H
]

$CFLAGS += " #{have_flags.map { |h| "-DHAVE_#{h}" }.join(' ')}"

# The openssl C extension uses macros expanding to defined
$CFLAGS += ' -Wno-expansion-to-defined'

MAC = `uname`.chomp == 'Darwin'

if MAC && !ENV['OPENSSL_PREFIX']
  ENV['OPENSSL_PREFIX'] = '/usr/local/opt/openssl'
end

if ENV['OPENSSL_PREFIX']
  $CFLAGS += " -I #{ENV['OPENSSL_PREFIX']}/include"
end

$LIBS += " -l libssl.#{RbConfig::CONFIG['NATIVE_DLEXT']}"

create_makefile('openssl')

_not_applied_have_flags = %w[
  ASSERT_H
  OPENSSL_SSL_H
  OPENSSL_CONF_API_H
  SSL_LIBRARY_INIT
  OPENSSL_SSL_H
  ERR_PEEK_LAST_ERROR
  ASN1_PUT_EOC
  BN_MOD_ADD
  BN_MOD_SQR
  BN_MOD_SUB
  CONF_GET1_DEFAULT_CONFIG_FILE
  EVP_CIPHER_CTX_SET_PADDING
  EVP_CIPHERFINAL_EX
  EVP_CIPHERINIT_EX
  EVP_DIGESTINIT_EX
  EVP_MD_CTX_CLEANUP
  EVP_MD_CTX_CREATE
  EVP_MD_CTX_DESTROY
  EVP_MD_CTX_INIT
  HMAC_CTX_CLEANUP
  HMAC_CTX_INIT
  PEM_DEF_CALLBACK
  RAND_EGD
  X509V3_SET_NCONF
  X509_CRL_ADD0_REVOKED
  X509_CRL_SET_ISSUER_NAME
  X509_CRL_SET_VERSION
  X509_CRL_SORT
  SSL_SESSION_GET_ID
  OPENSSL_CLEANSE
  SSLV2_METHOD
  SSLV2_SERVER_METHOD
  SSLV2_CLIENT_METHOD
  SSLV3_METHOD
  SSLV3_SERVER_METHOD
  SSLV3_CLIENT_METHOD
  TLSV1_1_METHOD
  TLSV1_1_SERVER_METHOD
  TLSV1_1_CLIENT_METHOD
  TLSV1_2_METHOD
  TLSV1_2_SERVER_METHOD
  TLSV1_2_CLIENT_METHOD
  SSL_CTX_SET_NEXT_PROTO_SELECT_CB
  SSL_SET_TLSEXT_HOST_NAME
  DH_GENERATE_PARAMETERS_EX
  DSA_GENERATE_PARAMETERS_EX
  RSA_GENERATE_KEY_EX
  EVP_CIPHER_CTX_FLAGS
  ST_FLAGS
  ST_ENGINE
  X509_ATTRIBUTE_SINGLE
  ST_SINGLE
  OPENSSL_FIPS
]
